package com.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.vo.LoginUser;



public class MyController<T> {

	@Autowired
	private HttpServletRequest request;
	
	
	/***
	 * session获取
	 * @return
	 */
	public HttpSession getSession(){
		return request.getSession();
	}
	
	/**
	 * 获取当前登录用户信息
	 * @return
	 */
	public LoginUser getUser() {
		LoginUser user = null;
		Object obj = getSession().getAttribute("loginUser");
		if(obj != null) {
			user = (LoginUser)obj;
		}
		return user;
	}
	
	/**
	 * 清除当前登录用户信息-
	 * @return
	 */
	public void removeUser() {
		getSession().setMaxInactiveInterval(0);
		getSession().removeAttribute("loginUser");
	}

	/**
	 * 返回存在session中的对象
	 * @param key
	 * @return
	 */
	public Object getSessionValue(String key) {
		HttpSession session = getSession();
		Object object = session.getAttribute(key);
		return object;
	}
	
	/**
	 * 返回存在session中的对象
	 * @param key
	 * @return
	 */
	public void setSessionValue(String key, Object object) {
		HttpSession session = getSession();
		session.setAttribute(key, object);
	}
	
	/**
	 * 返回分页
	 * @return
	 */
	public Page<T> getPage() {
		String page = request.getParameter("page");
		String limit = request.getParameter("limit");
		Integer p = page==null?1:Integer.parseInt(page);
		Integer l = limit==null?10:Integer.parseInt(limit);
		return new Page<>(p, l);
	}
	

}
